package com.ycz.master.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycz.master.mapper.UserMapper;
import com.ycz.master.pojo.User;
import com.ycz.master.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * author zyc
 * Created by sugar zhang on 2021/9/22 12:59
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper mapper;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    //查询所有用户
    @Override
    public List<User> findall() {
        List<User> alluser=new ArrayList<>();
        alluser = (List<User>) redisTemplate.opsForValue().get("allusers");
        if(alluser==null) {
           alluser=  mapper.selectList(null);
           redisTemplate.opsForValue().set("allusers",alluser,20, TimeUnit.SECONDS);
        }
        return alluser;

    }

    @Override
    public User fingbyid(Integer id) {
        User user = mapper.selectById(id);
        return user;
    }

    @Override
    public List<User> findbyname(String name, String idcard) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("true_name",name).or().eq("id_card",idcard);
        List<User> users = mapper.selectList(wrapper);
        return users;
    }


}
